package com.momei.service.impl;

import com.momei.dao.SysUserMapper;
import com.momei.entity.SysUser;
import com.momei.exception.BaseException;
import com.momei.security.BCryptPasswordEncoderUtil;
import com.momei.security.handler.JwtAuthenticationTokenFilter;
import com.momei.service.SysUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SysUserServiceImpl implements SysUserService {

    private static final Logger LOGGER = LoggerFactory.getLogger(SysUserServiceImpl.class);

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private BCryptPasswordEncoderUtil bCryptPasswordEncoderUtil;

    @Override
    public SysUser selectUserByUserName(String username) {
        return sysUserMapper.selectUserByUserName(username);
    }

    @Override
    public boolean checkLogin(String username, String password) {
        SysUser user = this.selectUserByUserName(username);
        if (user == null) {
            LOGGER.info("账户不存在！");
            throw new BaseException("账户不存在！");
        }else {
            String encodedPassword = user.getPassword();
            if(!bCryptPasswordEncoderUtil.matches(password, encodedPassword)) {
                LOGGER.info("密码不正确！");
                throw new BaseException("密码不正确！");
            }else{
                return  true;
            }
        }
    }
}
